package com.loveto.service;

import java.io.UnsupportedEncodingException;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.loveto.common.JsonUtils;
import com.loveto.constant.LovetoConstant;

public class SetupNoticeBodyActivity extends BaseActivity implements
View.OnClickListener {
	
	// 사용하는 변수들
	private ProgressDialog progressDialog;
	private Handler myHandler = new Handler();
	private int noticeID;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loveto_setup_notice_body);
		
		//변수 초기화
		progressDialog = new ProgressDialog(this);
		
		//클릭 리스너 달기
		((RelativeLayout)findViewById(R.id.btn_goto_back)).setOnClickListener(this);
		
		//전 인텐트에서 정보 받아오기
		Intent intent = getIntent();
		noticeID = intent.getIntExtra("noticeID", 1);
		
		if (isNetworkAvailableAndNotice())
		getNoticeBody();
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.btn_goto_back:
				finish();
				break;
		}
	}
	
	private void getNoticeBody() {
		progressDialog.setMessage("정보를 불러오고 있습니다.");
		progressDialog.show();
		Runnable runnable = new Runnable(){
			@Override
			public void run() {
				//UI 를 건들기 때문에 핸들러 사용.
				myHandler.post(new Runnable() {
					@Override
					public void run() {
							Log.d("tag", "getNoticeBody start");
							
							try {
								//서버에 정보 보내기
								String jsonText = JsonUtils.getStringFromUrl(LovetoConstant.SETUP_NOTICE_BODY + "?noticeid=" + noticeID);
								Log.d("tag", "jsonText : " + jsonText);
								//JSONArray jsonArray = new JSONArray(jsonText);
								JSONObject noticeObject = new JSONObject(jsonText);
								
								//화면에 적용하기
								((TextView)findViewById(R.id.notice_title)).setText(noticeObject.getString("title"));
								((TextView)findViewById(R.id.notice_body)).setText(noticeObject.getString("body"));
								
							} catch (JSONException e) {
								e.printStackTrace();
								Log.d("tag", "setRoundInfo() error, " + e.getMessage());
							} catch (UnsupportedEncodingException e) {
								e.printStackTrace();
								Log.d("tag", "setRoundInfo() error, " + e.getMessage());
							} finally {
							}
							progressDialog.dismiss();
							Log.d("tag", "getNoticeBody end");
							
						} //Handler run End
				});
			}
		};
		new Thread(runnable).start();
	}

}
